package _Banque;

import java.io.*;
import _Exception.*;


/**
 * La classe OperationBancaire fournit 
 * l'ensemble des moyens necessaires a la description des flux 
 * financiers entre les comptes bancaires des clients.
 * 
 * @authors J. Colin & N. Nagy
 * 
 * V1.0.0
 */
public class OperationBancaire implements Serializable
{ 
	private static final long serialVersionUID = -7205409329918449766L;
	
	private char    m_typeOperation; ///< N=Neutre, V=Versement, R=Retrait, I=remuneratIon
	private long	m_numeroOperation;
	private String  m_intituleOperation;
	private String  m_dateOperation;
	private float   m_montantOperation;

	/**
	 * Constructeur par defaut.
	 */
	public OperationBancaire(){
		m_typeOperation = 'N';
		m_numeroOperation = 0;
		m_intituleOperation = "---";
		m_dateOperation = _Banque.dateCourante();
		m_montantOperation = 0.f;
	}
	
	/**
	 * Constructeurs normal
	 */
	public OperationBancaire(final Banque banque ,char type, String intitule, float montant) throws IPException
	{
		if(type != 'V' && type != 'R' && type != 'I' && type != 'N')
			throw new IPException(1, "Type d'operation incorrect");
	    if(intitule == null || intitule.equals(""))
	    	throw new IPException(2);
	    if(montant < 0)
	    	throw new IPException(3, "Montant negatif");
		if (intitule.length() > 39)
			throw new IPException (2, "Intitule incorrect");
		
		m_typeOperation = type;
		m_dateOperation = _Banque.dateCourante();
		m_intituleOperation = intitule;
		m_montantOperation = montant;
		m_numeroOperation = banque.numeroRegistreOp();
	}

	/**
	 * Retourne le type d'operation
	 */
	public char getType(){
		return m_typeOperation;
	}
	
	/**
	 * Retourne le numero d'operation
	 */
	public long getNumero(){
		return m_numeroOperation;
	}
	
	/**
	 * Retourne l'intitule de l'operation
	 */
	public String getIntitule(){
		return m_intituleOperation;
	}
	
	/**
	 * Retourne la date de l'operation
	 */
	public String getDate(){
		return m_dateOperation;
	}
	
	/**
	 * Retourne le montant de l'operation
	 */
	public float getMontant(){
		return m_montantOperation;
	}
	
	/**
	 * toString
	 */
	public String toString(){
		if (m_typeOperation == 'N'){return "---";} // si un compte est neutre
		return "(" + m_dateOperation + "," + m_numeroOperation + "," + m_intituleOperation + "," + m_typeOperation + "," + m_montantOperation + " euros" + ")";
	}
	
	/**
	 * equals
	 */
	public boolean equals(Object obj){
		if (obj == null) return false;
		if (!(obj instanceof OperationBancaire)) return false;
		if (((OperationBancaire)obj).m_typeOperation == m_typeOperation &&			    // types egaux
			((OperationBancaire)obj).m_numeroOperation == m_numeroOperation &&		    // numeros d'operations egaux
			((OperationBancaire)obj).m_intituleOperation.equals(m_intituleOperation) &&	// intitule des operations egaux
			((OperationBancaire)obj).m_dateOperation.equals(m_dateOperation) &&			// dates des operations egales
			((OperationBancaire)obj).m_montantOperation == m_montantOperation)	        // montant des operations egaux
			{ return true; }
		return false;
	}
	
	/**
	 * clone
	 * Impossible de cloner une operation bancaire
	 */
	public Object clone() throws CloneNotSupportedException
	{
	    throw new CloneNotSupportedException(); 
	}
}
